package com.metamech.jabber.xml;

import java.util.LinkedList;

import org.apache.log4j.Logger;

import com.metamech.log.Log;
import com.metamech.vorpal.handler.RosterHandler;

/**
A simple class that queues up packets for processing, and processes them.
*/
public class PacketQueue {
	private static Logger logger=Logger.getLogger(PacketQueue.class);
	LinkedList queue = new LinkedList();
  
	/**
  	Push a packet onto the end of the queue.
	 */
	public synchronized void push(Packet packet){
		logger.info("[PQ] " + packet);
		queue.add(packet);
		notifyAll();
	}

	/**
  	Pull the packet at the head of the queue.  
  	@return The packet at the head of the queue, or null if no packets are available
	 */
	public synchronized Packet pull(){
		try {
			while (queue.isEmpty()) {
				wait();
			}
		} catch (InterruptedException e){
			return null;
		}
		return (Packet)queue.remove(0);
	}
}
